package id.co.shuba.spamobile.data;

import id.co.shuba.spamobile.App;
import id.co.shuba.spamobile.model.StandardInfo;
import id.co.shuba.spamobile.model.ProspectInfo;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class HolidayDB extends BaseDB {		
	
	public HolidayDB(DatabaseHandler db) {
		super(db);
	}
			
	public static void addOrUpdateOption(SQLiteDatabase db, String holidayDate, String holidayName, String date, String isActive) {
		String[] params = new String[] { holidayDate, holidayName, date, isActive };
		db.execSQL("INSERT OR REPLACE INTO TBL_MS_HOLIDAY(HOLIDAY_DATE, HOLIDAY_NAME, UPDATE_DATE, IS_ACTIVE) VALUES(?,?,?,?)", params);		
	}
	
	public static int dateCompare(Calendar c1, Calendar c2) {		
		if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR)) 
	        return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
	    if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH)) 
	        return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
	    return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
	}
	
	public Date addDays2(Date date, int value) {
		SQLiteDatabase db = _db.getReadableDatabase();
		return addDays2(db, date, value);
	}
	
	public static Date addDays2(SQLiteDatabase db, Date date, int value) {
		int direction = value < 0? -1 : 1;
		Calendar start = GregorianCalendar.getInstance();
		start.setTime(date);
		List<Date> holidays = getList(db);
		Calendar holiday = GregorianCalendar.getInstance();
		
		while (value != 0) {
			start.add(Calendar.DAY_OF_MONTH, direction);
			
			boolean isHoliday = false;
			Date t = null;
			for (Date h : holidays) {
				holiday.setTime(h);
				if (dateCompare(start, holiday) == 0) {
					isHoliday = true;									
					t = h;
					break;
				}					
			}
			
			if (isHoliday)
				holidays.remove(t);
			
			if (start.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
					&& start.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
					&& !isHoliday)
				
				value -= direction;
		}
		
		return start.getTime();
	}
	
	public String addDays(Date date, int value) {
		SQLiteDatabase db = _db.getReadableDatabase();
		return addDays(db, date, value);
	}
	
	public static String addDays(SQLiteDatabase db, Date date, int value) {			
		return App.dateTime.format(addDays2(db, date, value));
	}
	
	public List<java.util.Date> getList() {
		SQLiteDatabase db = _db.getReadableDatabase();
		return getList(db);
	}
	
	public static List<java.util.Date> getList(SQLiteDatabase db ) {
		String sql = "SELECT HOLIDAY_DATE FROM TBL_MS_HOLIDAY WHERE IS_ACTIVE = '1'";		
		
		Cursor cursor = db.rawQuery(sql, null);

		List<java.util.Date> list = new ArrayList<java.util.Date>();

		if (cursor.moveToFirst()) {
			do {
				java.util.Date date = null;
				
				try {
					date = App.dateTime.parse(cursor.getString(0));
					list.add(date);
				} catch (ParseException e) {					
					e.printStackTrace();
				}
								
			} while (cursor.moveToNext());
		}

		cursor.close();

		return list;
	}
	
	public static String getMaxDate(SQLiteDatabase db) {		
		Cursor cursor = db.rawQuery(
				"SELECT MAX(UPDATE_DATE) UPDATE_DATE FROM TBL_MS_HOLIDAY", null);

		if (cursor != null && cursor.getCount() > 0)
			cursor.moveToFirst();
		else {
			cursor.close();
			return null;
		}
		
		String result = cursor.getString(cursor.getColumnIndex("UPDATE_DATE"));		
		cursor.close();

		return result;
	}	
}
